' This routine prompts the user to enter a string and then generates the corresponding
' key press commands for use in a macro in a configuration file for Soarer's converter
' for adapting a keyboard with a PS/2 interface cable to a USB port.  You can copy
' and paste the resulting code into a Soarer configuration file or use an include statement.
'
' This code is written in Visual Basic for Applications for use in a Microsoft Word
' document.  You can press Alt F11 while in Microsoft Word to enter the Visual Basic
' environment.  You can then select Module from the Insert menu and copy this code into
' the module and run the GeneratePresses() routine.  Depending on what version of
' Microsoft Word you have and what security settings are in place, you might need to
' enable macros.  Some versions of Word might require you to save the Word document
' with a *.docm file name extension to be able to execute Visual Basic macros.
'
' 01 May 2020 Edward Lee.
'
Sub GeneratePresses()
  Dim s As String
  Dim c As String
  Dim i As Integer
  Dim Shifted As Boolean
  Dim CharacterCount As Integer
  
  Shifted = False
  CharacterCount = 0
  
  s = InputBox("Enter a string:")
  
  For i = 1 To Len(s)
    c = Mid(s, i, 1)
    
    ' Is the character in upper case or shifted on the keyboard?
    If (c >= "A" And c <= "Z") Or c = "~" Or c = "!" Or c = "@" Or c = "#" Or c = "$" Or c = "%" Or c = "^" Or c = "&" Or c = "*" Or c = "(" Or c = ")" Or c = "_" Or c = "+" Or c = "{" Or c = "}" Or c = "|" Or c = ":" Or c = """" Or c = "<" Or c = ">" Or c = "?" Then
      ' If the shift state is not active, then activate shift
      If Not Shifted Then
        Shifted = True
        ActiveDocument.Content.InsertAfter "    set_meta LSHIFT" & Chr(13) & Chr(10)
      End If
    Else
      ' If the shift state is active, then deactivate shift
      If Shifted Then
        Shifted = False
        ActiveDocument.Content.InsertAfter "    clear_meta LSHIFT" & Chr(13) & Chr(10)
      End If
    End If
    
    ActiveDocument.Content.InsertAfter "    press " & Name(c) & Chr(13) & Chr(10)
    CharacterCount = CharacterCount + 1
  Next i
  
  ' If the shift state is active, then deactivate shift
  If Shifted Then
    Shifted = False
    ActiveDocument.Content.InsertAfter "    clear_meta LSHIFT" & Chr(13) & Chr(10)
  End If
  
  If CharacterCount > 63 Then ActiveDocument.Content.InsertAfter "  # Warning: The string is too long, requiring " & CharacterCount & " PRESS commands, which exceeds the limit of 63."
  
End Sub

' Given a character on a USA keyboard, this function
' returns the name to use in a PRESS command.
Function Name(c As String) As String
  Select Case c
    Case " "
      Name = "SPACE"
    Case "!"
      Name = "1" & Chr(9) & "# ! Exclamation"
    Case """"
      Name = "QUOTE" & Chr(9) & "# "" Double quote"
    Case "#"
      Name = "3" & Chr(9) & "# # Number sign"
    Case "$"
      Name = "4" & Chr(9) & "# $ Dollar sign"
    Case "%"
      Name = "5" & Chr(9) & "# % Percent"
    Case "&"
      Name = "7" & Chr(9) & "# & Ampersand"
    Case "'"
      Name = "QUOTE" & Chr(9) & "# ' Apostrophe or single quote"
    Case "("
      Name = "9" & Chr(9) & "# ( Opening parenthesis"
    Case ")"
      Name = "0" & Chr(9) & "# ) Closing parenthesis"
    Case "*"
      Name = "8" & Chr(9) & "# Asterisk"
    Case "+"
      Name = "EQUAL" & Chr(9) & "# Plus"
    Case ","
      Name = "COMMA" & Chr(9) & "# ,"
    Case "-"
      Name = "MINUS" & Chr(9) & "# -"
    Case "."
      Name = "PERIOD" & Chr(9) & "# ."
    Case "/"
      Name = "SLASH" & Chr(9) & "# / Forward slash"
    Case "0"
      Name = "0"
    Case "1"
      Name = "1"
    Case "2"
      Name = "2"
    Case "3"
      Name = "3"
    Case "4"
      Name = "4"
    Case "5"
      Name = "5"
    Case "6"
      Name = "6"
    Case "7"
      Name = "7"
    Case "8"
      Name = "8"
    Case "9"
      Name = "9"
    Case ":"
      Name = "SEMICOLON" & Chr(9) & "# : Colon"
    Case ";"
      Name = "SEMICOLON" & Chr(9) & "# ;"
    Case "<"
      Name = "COMMA" & Chr(9) & "# < Less than"
    Case "="
      Name = "EQUAL" & Chr(9) & "# ="
    Case ">"
      Name = "PERIOD" & Chr(9) & "# > Greater than"
    Case "?"
      Name = "SLASH" & Chr(9) & "# ? Question mark"
    Case "@"
      Name = "2" & Chr(9) & "# @ At sign"
    Case "A"
      Name = "A"
    Case "B"
      Name = "B"
    Case "C"
      Name = "C"
    Case "D"
      Name = "D"
    Case "E"
      Name = "E"
    Case "F"
      Name = "F"
    Case "G"
      Name = "G"
    Case "H"
      Name = "H"
    Case "I"
      Name = "I"
    Case "J"
      Name = "H"
    Case "K"
      Name = "K"
    Case "L"
      Name = "L"
    Case "M"
      Name = "M"
    Case "N"
      Name = "N"
    Case "O"
      Name = "O"
    Case "P"
      Name = "P"
    Case "Q"
      Name = "Q"
    Case "R"
      Name = "R"
    Case "S"
      Name = "S"
    Case "T"
      Name = "T"
    Case "U"
      Name = "U"
    Case "V"
      Name = "V"
    Case "W"
      Name = "W"
    Case "X"
      Name = "X"
    Case "Y"
      Name = "Y"
    Case "Z"
      Name = "Z"
    Case "["
      Name = "LEFT_BRACE" & Chr(9) & "# [ Opening square brace"
    Case "\"
      Name = "BACKSLASH" & Chr(9) & "# \"
    Case "]"
      Name = "RIGHT_BRACE" & Chr(9) & "# ] Closing square brace"
    Case "^"
      Name = "6" & Chr(9) & "# ^ Carat"
    Case "_"
      Name = "MINUS" & Chr(9) & "# _ Underline"
    Case "`"
      Name = "BACK_QUOTE" & Chr(9) & "# `"
    Case "a"
      Name = "a"
    Case "b"
      Name = "b"
    Case "c"
      Name = "c"
    Case "d"
      Name = "d"
    Case "e"
      Name = "e"
    Case "f"
      Name = "f"
    Case "g"
      Name = "g"
    Case "h"
      Name = "h"
    Case "i"
      Name = "i"
    Case "j"
      Name = "j"
    Case "k"
      Name = "k"
    Case "l"
      Name = "l"
    Case "m"
      Name = "m"
    Case "n"
      Name = "n"
    Case "o"
      Name = "o"
    Case "p"
      Name = "p"
    Case "q"
      Name = "q"
    Case "r"
      Name = "r"
    Case "s"
      Name = "s"
    Case "t"
      Name = "t"
    Case "u"
      Name = "u"
    Case "v"
      Name = "v"
    Case "w"
      Name = "w"
    Case "x"
      Name = "x"
    Case "y"
      Name = "y"
    Case "z"
      Name = "z"
    Case "{"
      Name = "LEFT_BRACE" & Chr(9) & "# { Opening curly brace"
    Case "|"
      Name = "BACKSLASH" & Chr(9) & "# | Vertical Pipe"
    Case "}"
      Name = "RIGHT_BRACE" & Chr(9) & "# Closing curly brace"
    Case "~"
      Name = "BACK_QUOTE" & Chr(9) & "# ~ Tilde"
    Case Else
      Name = "UNASSIGNED"
  End Select
End Function
